home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 5 / CD_Magazyn_EXEC_nr_5.iso / eXec / Krotkie opisy / Programy / XADMaster / xad_bzip2.lha / ConvertE.c < prev    next >
C/C++ Source or Header  |  2000-08-05  |  3KB  |  87 lines

  1. #ifndef XADMASTER_CONVERTE_C
  2. #define XADMASTER_CONVERTE_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        ConvertE.c
  7.     Main:        xadmaster
  8.     Versionstring:    $VER: ConvertE.c 1.2 (04.07.2000)
  9.     Author:        SDI, Kyzer
  10.     Distribution:    Public Domain
  11.     Description:    endian conversion macros
  12.  
  13.  1.0   22.08.1999 : first version
  14.  1.1   22.06.2000 : improved version by Kyzer, removed EndSet funcs
  15.  1.2   04.07.2000 : more compiler checks, portability fixes
  16. */
  17.  
  18. /* macros to use:
  19.  * EndGetXXX(a)  - reads value of endianness XXX from memory.
  20.  *                 'a' should be of type 'char *' or 'unsigned char *'
  21.  * EndConvXXX(a) - converts expression/value/var of endianness XXX to
  22.  *                 the correct endianness for this architecture.
  23.  *
  24.  * XXX can be:
  25.  * M32 - big-endian Motorola format, 32 bit value
  26.  * M16 - big-endian Motorola format, 16 bit value
  27.  * I32 - little-endian Intel format, 32 bit value
  28.  * I16 - little-endian Intel format, 16 bit value
  29.  *
  30.  * Note that the EndConvXXX macros only work for architectures that are
  31.  * themselves little- or big-endian, and only work if the defines tested
  32.  * below reveal the correct endianness of the architecture being compiled for.
  33.  * Define either XAD_BIGENDIAN or XAD_LITTLEENDIAN.
  34.  *
  35.  * It is recommended that you only use the EndGetXXX() macros, as these are
  36.  * endian-neutral, and even work on middle-endian machines like PDPs or
  37.  * switchable-endian machines like PPCs.
  38.  *
  39.  * Keep in mind, that the macros require calculation time, so avoid to use
  40.  * them double time. Call them once and reuse results.
  41.  */
  42.  
  43. #define EndGetM32(a)  ((((a)[0])<<24)|(((a)[1])<<16)|(((a)[2])<<8)|((a)[3]))
  44. #define EndGetM16(a)  ((((a)[0])<<8)|((a)[1]))
  45. #define EndGetI32(a)  ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
  46. #define EndGetI16(a)  ((((a)[1])<<8)|((a)[0]))
  47.  
  48. /* private */
  49. #define _xecswap16(a) ((((a) & 0x00FF) <<  8) | (((a) >>  8) & 0x00FF))
  50. #define _xecswap32(a) ((((a) & 0x00FF) << 24) | (((a) & 0xFF00) <<  8) |  \
  51.                        (((a) >>  8) & 0xFF00) | (((a) >> 24) & 0x00FF))
  52. /* some future music */
  53. #define _xecswap64(a) (_xecswap32((a) >> 32) | (_xecswap32(a) << 32))
  54.  
  55.  
  56. /* if XAD_BIGENDIAN or XAD_LITTLEENDIAN is defined, use that instead of
  57.  * making compiler tests. Otherwise, all Unix systems endianness can be
  58.  * tested with the autoconf AC_C_BIGENDIAN test (defines WORDS_BIGENDIAN),
  59.  * otherwise it's generally safe to assume little-endianness due to the
  60.  * extreme number of Intel PCs and compilers out there. Big-endian non-unix
  61.  * exceptions would be Amiga or 68k Apple Macintosh (MPW compiler or
  62.  * CodeWarrior)
  63.  */
  64. #if !defined(XAD_BIGENDIAN) && !defined(XAD_LITTLEENDIAN)
  65. # if defined(WORDS_BIGENDIAN) || defined(AMIGA) || defined(__SC__) || defined(__MWERKS__)
  66. #  define XAD_BIGENDIAN
  67. # else
  68. #  define XAD_LITTLEENDIAN
  69. # endif
  70. #endif
  71.  
  72. #if defined(XAD_BIGENDIAN)
  73. # define EndConvM32(a)    (a)
  74. # define EndConvM16(a)    (a)
  75. # define EndConvI32(a)    _xecswap32(a)
  76. # define EndConvI16(a)    _xecswap16(a)
  77. #elif defined(XAD_LITTLEENDIAN)
  78. # define EndConvM32(a)    _xecswap32(a)
  79. # define EndConvM16(a)    _xecswap16(a)
  80. # define EndConvI32(a)    (a)
  81. # define EndConvI16(a)    (a)
  82. #else
  83. # error Either XAD_BIGENDIAN or XAD_LITTLEENDIAN must be defined!
  84. #endif
  85.  
  86. #endif /* XADMASTER_CONVERTE_C */
  87.